package com.wlhse.util.PoiCheckItemV2;


import com.wlhse.entity.CheckItemStardardPojo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 检查项/检查标准导入
 * @Author: hg
 * @Date: 2019/6/5
 */
public class PoiTreeCheckItem extends PoiTreeUtil {

    //泛型保证所有类型相同
    private List checkItemStandardList;

    public PoiTreeCheckItem(Poi poi, String path, Class c) {
        super(poi, path, c);
        checkItemStandardList = new ArrayList<>();
    }

    @Override
    protected String getCode(String value, String[] cache, int offset, int step) {
        String tmp = cache[offset + 1];
        //当前没缓存 || 父节点已更新
        if (null == tmp || !tmp.substring(0, cache[offset].length()).equals(cache[offset])) {
            tmp = CodeUtil.maxCode(codes, cache[offset], step);
        }
        if ("".equals(tmp)) {
            tmp = cache[offset];
            for (int i = 0; i < step - 1; i++)
                tmp += "0";
            setDefault(tmp + "0");
            //0000 0000
            //0000 0000 0000
            tmp += "1";
        } else
            tmp = CodeUtil.codeInc(tmp);
        cache[offset + 1] = tmp;
        //添加检查标准
        if (offset + 1 == cache.length - 1)
            checkItemStandardList.add(getT(tmp, value));
        return tmp;
    }

    @Override
    protected void checkDefault(String str, Map map, int step) {
        StringBuilder strBuilder = new StringBuilder(str);
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < step; i++) {
                strBuilder.append("0");
            }
            str = strBuilder.toString();
            if (!map.containsKey(str)) {
                //添加code
                codes.add(str);
                //添加list
                result.add(getT(str, "其它"));
            }
        }
    }

    @Override
    protected void setDefault(String code) {
        result.add(getT(code, "其它"));
    }

    public List<CheckItemStardardPojo> getCheckItemStandardList() {
        return checkItemStandardList;
    }
}
